CREATE TABLE [dbo].[tb_CongresosSeminarios]
(
[IdCongresoSeminario] [int] NOT NULL IDENTITY(1, 1),
[Agente] [int] NOT NULL,
[Duracion] [int] NULL,
[UnidadDeTiempo] [int] NULL,
[TipoDeEvento] [int] NULL,
[TipoDeParticipacion] [int] NULL,
[DescripcionDelEvento] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EntidadOrganizadora] [int] NULL,
[TipoEntidad] [int] NULL,
[GrupoIncumbencias] [int] NULL,
[FechaDelEvento] [datetime] NULL,
[FechaDeAlta9] [datetime] NULL CONSTRAINT [DF_tb_CongresosSeminarios_FechaDeAlta9] DEFAULT (getdate()),
[FechaPresentacion] [datetime] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[AsignarFechaDefaultCongresosSeminarios] ON [dbo].[tb_CongresosSeminarios] 
FOR INSERT, UPDATE
AS
BEGIN
--AsignarFechaDefault
SET NOCOUNT ON

DECLARE @FechaPresentacion as datetime
DECLARE @id as int


SELECT @FechaPresentacion = FechaPresentacion,@id= idCongresoSeminario  FROM INSERTED

	IF (@@ROWCOUNT=1) 
	 BEGIN
	    IF @FechaPresentacion IS NULL SELECT @FechaPresentacion= dbo.FECHA_PRESENTACION_DEFAULT ()
	    UPDATE tb_CongresosSeminarios SET FechaPresentacion = @FechaPresentacion WHERE idCongresoSeminario=@id
	 END

END


GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] ADD CONSTRAINT [PK_tb_CongresosSeminarios] PRIMARY KEY CLUSTERED  ([IdCongresoSeminario]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idxCongresosAgente] ON [dbo].[tb_CongresosSeminarios] ([Agente]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_Agentes] FOREIGN KEY ([Agente]) REFERENCES [dbo].[tb_Agentes] ([idAgente])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_EntidadesCapacitadoras] FOREIGN KEY ([EntidadOrganizadora]) REFERENCES [dbo].[tb_EntidadesCapacitadoras] ([idEntidad])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_GruposDeIncumbencias] FOREIGN KEY ([GrupoIncumbencias]) REFERENCES [dbo].[tb_GruposDeIncumbencias] ([idGrupoIncumbencia])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_TiposDeEventos] FOREIGN KEY ([TipoDeEvento]) REFERENCES [dbo].[tb_TiposDeEventos] ([IdEvento])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_TiposDeParticipacionEnCongresos] FOREIGN KEY ([TipoDeParticipacion]) REFERENCES [dbo].[tb_TiposDeParticipacionEnCongresos] ([IdTipoDeParticipacion])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_TipoEntidadCapacitadora] FOREIGN KEY ([TipoEntidad]) REFERENCES [dbo].[tb_TipoEntidadCapacitadora] ([id])
GO
ALTER TABLE [dbo].[tb_CongresosSeminarios] WITH NOCHECK ADD CONSTRAINT [FK_tb_CongresosSeminarios_tb_UnidadDeTiempo] FOREIGN KEY ([UnidadDeTiempo]) REFERENCES [dbo].[tb_UnidadDeTiempo] ([idUnidad])
GO
